<script setup lang="ts">
import { onMounted, reactive, ref } from "vue";
import axios from "axios";
import { ElMessage } from 'element-plus';

const tableData = reactive([])
const tagList = ref([])
const showAdd = ref(false)
const selectedTag = ref('')

// 添加分页相关数据
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)

// 修改：为 addForm 添加响应式引用
const addForm = ref({
  relatedId: '',
  tagId: '',
})

const getTagList = () => {
  axios.get('/admin/tag/taglist?type=1').then(res => {
    tagList.value = res.data
  })
}

// 添加：获取表格数据的函数
const getTableData = () => {
  axios.get(`/admin/tag/post-tags?page=${currentPage.value}&size=${pageSize.value}`).then(res => {
    tableData.length = 0
    // 使用 records 而不是 list
    tableData.push(...res.data.records)
    total.value = res.data.total
    console.log(tableData)
  })
}

const handlePageChange = (page: number) => {
  currentPage.value = page
  getTableData()
}

const handleSizeChange = (size: number) => {
  pageSize.value = size
  currentPage.value = 1
  getTableData()
}

onMounted(() => {
  getTagList()
  getTableData()
})

// 添加：表单提交函数
const submitForm = () => {
  // 将 selectedTag 的值同步到 addForm
  addForm.value.tagId = selectedTag.value
  console.log('提交表单:', addForm.value)

  // 添加实际提交逻辑
  axios.post('/admin/tag/post-tags/add', addForm.value).then(res => {
    console.log(res)
    if (res.code === 200) {
      showAdd.value = false
      // 提交成功后重新加载数据
      getTableData()
      ElMessage.success('添加成功')
      // 重置表单
      addForm.value.relatedId = ''
      addForm.value.tagId = ''
      selectedTag.value = ''
    }
  }).catch(err => {
    ElMessage.error('添加失败')
    console.error(err)
  })
}

const del = (row: any) => {
  axios.delete(`/admin/tag/prefer-tags/del?id=${row.id}`).then(res => {
    if(res.code === 200){
      ElMessage.success('删除成功')
      getTableData()
    }
  }).catch(err => {

    console.error(err)
  })
}
</script>

<template>
  <!-- 修改：将按钮类型设置为primary -->
  <el-button type="primary" @click="showAdd = true">添加</el-button>

  <!-- 添加：标签展示表格 -->
  <el-table :data="tableData" style="width: 100%; margin-top: 20px;">
    <el-table-column prop="id" label="ID" width="180" />
    <el-table-column prop="relatedId" label="文章ID" width="180" />
    <el-table-column prop="tagName" label="标签名" />
    <el-table-column label="标签类型" prop="type">
      <template #default="scope">
        {{ scope.row.type === 1 ? '帖子标签' : scope.row.type === 2 ? '偏好标签' : '' }}
      </template>
    </el-table-column>
    <el-table-column label="操作" width="150">
      <template #default="scope">
        <el-button size="small" type="danger" @click="del(scope.row)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>

  <!-- 添加：分页组件 -->
  <el-pagination
      v-model:current-page="currentPage"
      v-model:page-size="pageSize"
      :page-sizes="[10, 20, 50, 100]"
      :total="total"
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="handleSizeChange"
      @current-change="handlePageChange"
      style="margin-top: 20px; justify-content: flex-end;"
  />

  <el-dialog v-model="showAdd" title="添加标签">
    <el-form>
      <!-- 添加：postId 输入字段 -->
      <el-form-item label="文章ID">
        <el-input v-model="addForm.relatedId" placeholder="请输入文章ID"></el-input>
      </el-form-item>

      <el-form-item label="标签名">
        <el-select v-model="selectedTag" placeholder="请选择标签">
          <el-option
              v-for="tag in tagList"
              :key="tag.id"
              :label="tag.tagName"
              :value="tag.id">
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>

    <!-- 添加：对话框底部按钮 -->
    <template #footer>
      <el-button @click="showAdd = false">取消</el-button>
      <el-button type="primary" @click="submitForm">确定</el-button>
    </template>
  </el-dialog>
</template>

<style scoped>

</style>
